import React,{Component} from 'react'
import {Link} from 'react-router';
import * as pub from '../../config/public';
class Menu extends Component {
	constructor(args) {
		super()
		this.state = {
			activeIndex: -1,
			menuItems: [
			// {
			// 	href: '/admin',
			// 	text: '图书管理',
			// }, {
			// 	href: '/admin/MessgeManage',
			// 	text: '任务发布',
			// }, {
			// 	href: '/admin/CourseManage',
			// 	text: '课程管理',
			// }, {
			// 	href: '/admin/ArticleManage',
			// 	text: '蜂行圈管理',
			// }, {
			// 	href: '/admin/BroadManagement',
			// 	text: '直播管理'
			// }, {
			// 	href: '/admin/QuestionManage',
			// 	text: '综合试题管理',
			// }, {
			// 	href: '/admin/PointShopManage',
			// 	text: '积分商城管理',
			// }, {
			// 	href: '/admin/PointManage',
			// 	text: '会员积分管理',
			// }, 
			// // {
			// // 	href: '/admin/DataAnalysis',
			// // 	text: '数据分析',
			// // }, 
			// {
			// 	href: '/admin/BasisSet',
			// 	text: '基础设置',
			// }, 
			],
			isLogin: false
		}
	}
	checkPage(page) {
		if(pub.user === null){
			return;
		}
		let admin_menu = pub.user.menu.admin_menu;
		let menuItems = [];
		for(let x of admin_menu){
			menuItems.push({
				href:x.runscript,
				text:x.nodeName
			})
		}
		this.setState({
			menuItems
		})

		let activeIndex = 0;
		for (let x of menuItems) {
			if (x.href === page) {
				break;
			}
			activeIndex++;
		}
		
		if (page.indexOf('admin/CourseExamine') !== -1) {
			activeIndex = 1;
		}
		if (page.indexOf('admin/PointDetail') !== -1) {
			activeIndex = 6;
		}
		if (page.indexOf('admin/ArticleDetail') !== -1) {
			activeIndex = 3;
		}
		if (page.indexOf('admin/ArticleExamine') !== -1) {
			activeIndex = 3;
		}
		if (page.indexOf('admin/QuestionDetail/') !== -1) {
			activeIndex = 4;
		}
		if (page.indexOf('admin/AddQuestion') !== -1) {
			activeIndex = 4;
		}

		this.setState({
			activeIndex: activeIndex
		})
	}
	componentWillMount() {
		this.checkPage(this.props.page);
	}
	componentWillReceiveProps(nextProps) {
		this.checkPage(nextProps.page)
	}
    render(){
    	return(
			  <div className="menu-left">
				<ul>
					{
		           	this.state.menuItems.map((item, index) => {
		           		let className = this.state.activeIndex === index ? 'active' : '';
		           		return (
		           			<li key={index}>
		           			<Link className={className} 
		           			onClick={()=>{this.setState({activeIndex:index})}}
		           			 to={item.href}>{item.text}</Link></li>
		           		)
		           	})
		           }
				</ul>
			</div>
    		)
    }
}
export default Menu;